package com.qf.team2.netdisc.service.impl;

import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.dao.ShowDocumentMapper;
import com.qf.team2.netdisc.service.ShowDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件查询服务接口实现类
 *
 * @author Simon_Wong
 * @date 2021/05/26 11:05
 */
@Service
public class ShowDocumentServiceImpl implements ShowDocumentService {

    @Autowired
    private ShowDocumentMapper showDocumentMapper;

    @Override
    public List<Document> getDocumentListByIdAndName(Integer userId) {
        Document document = showDocumentMapper.getDocumentById(userId);
        String documentName = document.getName();
        String documentUrl = document.getUrl() + "\\";

        ArrayList<Document> documentList = new ArrayList<>();

        List<Document> list = showDocumentMapper.getDocumentListByIdAndName(userId, documentName);
        for (int i = 0; i < list.size(); i++) {
            String url = list.get(i).getUrl();

            String ddd = url.replace(documentUrl, "DDD");

            boolean contains = ddd.contains("\\");

            if (!contains) {
                documentList.add(list.get(i));
            }
        }

        return documentList;
    }

    @Override
    public List<Document> getDocumentListByDocumentId(Integer documentId) {
        Document document = showDocumentMapper.getDocumentListByIdAndDocumentId(documentId);
        String url = document.getUrl();
        Integer userId = document.getUserId();
        System.out.println(url);
        String[] split = url.split("\\\\");
        String urlList = "";
        for (int i = 0; i < split.length; i++) {
            if (i == split.length - 1) {
                urlList += split[i];
            } else {
                urlList += split[i] + "\\\\\\\\";
            }

        }

        String documenturl = url + "\\";

        ArrayList<Document> documentList = new ArrayList<>();

        List<Document> list = showDocumentMapper.getDocumentListByIdAndUrl(userId, urlList);
        for (int i = 0; i < list.size(); i++) {
            String urlList1 = list.get(i).getUrl();

            String ddd = urlList1.replace(documenturl, "DDD");

            boolean contains = ddd.contains("\\");

            if (false == contains) {
                documentList.add(list.get(i));
            }
        }

        return documentList;
    }

    @Override
    public List<Document> getDocumentListByMD5(String md5) {
        List<Document> document = showDocumentMapper.getDocumentListByMD5(md5);

        return document;
    }
}
